﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.SharePoint.QueryBuilder (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Utilities;

namespace Sapiens.at.SharePoint.Utils
{
    /// <summary>
    /// Represents a CAML expression without a value to compare. These are "IsNull" and "IsNotNull".
    /// The <see cref="QueryBuilderValueOperator{T}"/> compares a field with a value.
    /// </summary>
    /// <seealso cref="QueryBuilderValueOperator{T}"/>
    public class QueryBuilderOperator : IQueryBuilderExpression
    {
        /// <summary>
        /// Gets or sets the type of the expression (i.e. "Eq", "BeginsWith", ...).
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        /// Gets or sets the field name for the operator.
        /// </summary>
        public string FieldRef { get; set; }

        /// <summary>
        /// Instantiates a new operator.
        /// </summary>
        /// <param name="type">The <see cref="Type"/> of the expression</param>
        /// <param name="fieldRef">The <see cref="FieldRef"/> of the expression</param>
        public QueryBuilderOperator(string type, string fieldRef)
        {
            if (string.IsNullOrEmpty(type)) { throw new ArgumentNullException("type"); }
            if (string.IsNullOrEmpty(fieldRef)) { throw new ArgumentNullException("fieldRef"); }

            Type = type;
            FieldRef = fieldRef;
        }

        /// <summary>
        /// Converts the expression into the CAML representation.
        /// </summary>
        /// <returns>The CAML query for this expression</returns>
        public override string ToString()
        {
            return string.Format("<{0}><FieldRef Name=\"{1}\"/></{0}>", Type, FieldRef);
        }

        /// <summary>
        /// Allways returns false (can't be empty)
        /// </summary>
        /// <returns>false</returns>
        public bool IsEmpty() { return false; }
    }
}
